import pygame
from pygame.locals import *
from Ksink import load_graphics, load_dice_graphics
from players import Player
from gordon.objects import State
from Field import Field

class Arena(State):
	def __init__(self, screen):
		State.__init__(self, 'Arena')
		self.screen = screen
		self.images = load_graphics()
		self.dice_images = load_dice_graphics()
		self.field = Field(screen, self.images)
		self.images = load_graphics()
		self.dice_images = load_dice_graphics()
		self.field.add_stock_player((2,4))
		self.field.add_stock_player((3,5))
		self.field.add_stock_player((3,6))
		self.field.add_stock_player((24,7))
		self.field.add_stock_player((1,1))
		self.field.build_path_map()
		self.selected = None
		self.mode = None
		
	def update(self):
		#print self.mode, self.selected
		for event in pygame.event.get():
			if event.type == pygame.QUIT: self.run = False
			elif event.type == MOUSEBUTTONUP:
				mpos = event.pos
				mr = self.field.map_rect
				field_pos = ((mpos[0]-mr.x)/16,(mpos[1]-mr.y)/16)
				# If there is no mode 
				if self.mode == None:
					found = self.field.check_for_player(field_pos)	
					if found:
						self.selected = found
						self.mode = 'move'	
					else:
						self.selected = None
						self.mode = None
						
				#current mode is Move
				elif self.mode == 'move':
					if self.selected:
						found = self.selected.is_adj(field_pos)
						if found:
							self.selected.move_to(found)
							self.field.build_path_map()
							self.mode = 'move'	
						else:
							found = self.field.check_for_player(field_pos)
							if found:
								self.selected = found
								self.mode = 'move'	
							else:
								self.mode = None
								#self.selected = None
							
				#current mode is Hit
				elif self.mode == 'hit':
					if self.selected:
						found = self.selected.is_adj(field_pos)
						if self.field.check_for_player(found):
							print self.selected, "hit", self.field.check_for_player(found)
				#check menu buttons. Set modes.
				if self.selected:
					self.check_menu(mpos)
						
			elif event.type == KEYDOWN:
				if event.key == K_ESCAPE:
					self.run = False
				if event.key == K_q:
					self.selected = None
					self.mode = None
					
	def draw(self):
		self.field.draw_field()
		self.field.draw_players()
		if self.selected:
			self.draw_selected()
		self.screen.blit(self.images['ball'], (	self.field.ball.loc[0]*16+self.field.map_rect.x, 
												self.field.ball.loc[1]*16+self.field.map_rect.y, 16,16
											   ))
		#screen.blit(self.images['team1'], (3*16+self.field.map_rect.x, 4*16+self.map_rect.y, 16,16))
		self.draw_menu()
		pygame.display.flip()

	def draw_menu(self):
		self.screen.blit(self.images['move_icon'], (30,260,32,32))
		self.screen.blit(self.images['hit_icon'], (64,260,32,32))
		self.screen.blit(self.images['pickup_icon'], (96,260,32,32))
		if self.mode == 'move':
			self.screen.blit(self.images['selected_icon'], (30,260,32,32))
		elif self.mode == 'hit':
			self.screen.blit(self.images['selected_icon'], (64,260,32,32))
			


	
	
	def draw_selected(self):
		self.screen.blit(self.images['move_sq'], (	self.selected.loc[0]*16+self.field.map_rect.x, 
														self.selected.loc[1]*16+self.field.map_rect.y, 16,16
													  ))
		if self.mode == 'move':
			if self.selected.has_moves():
				for s in self.selected.get_adj():
					self.screen.blit(self.images['move_sq'], (	s[0]*16+self.field.map_rect.x, 
																s[1]*16+self.field.map_rect.y, 16,16
															  ))
		elif self.mode == 'hit':
			for s in self.selected.get_adj():
					self.screen.blit(self.images['hit_sq'], (	s[0]*16+self.field.map_rect.x, 
																s[1]*16+self.field.map_rect.y, 16,16
															  ))
	def check_menu(self, mpos):
		if mpos[0] >= 30 and mpos[0] <= 30 + 32:
			if mpos[1] >= 260 and mpos[1] <= 260 + 32:
				self.mode = "move"
		if mpos[0] >= 64 and mpos[0] <= 64 + 32:
			if mpos[1] >= 260 and mpos[1] <= 260 + 32:
				self.mode = "hit"